package aquactl.views;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import aquactl.Application;
import aquactl.comapi.ComException;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class InfoView extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "aquactl.views.InfoView";

	private Action sychClockAction;

	/**
	 * The constructor.
	 */
	public InfoView() {
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.makeColumnsEqualWidth = true;
		parent.setLayout(gridLayout);

		Group group = new Group(parent, SWT.SHADOW_ETCHED_IN);
		group.setText("Informacje");

		gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.makeColumnsEqualWidth = false;
		group.setLayout(gridLayout);

		Label label = new Label(group, SWT.NONE);
		label.setText("Czas:");

		label = new Label(group, SWT.NONE);
		try {
			label.setText(Application.getComApi().getClock());
		} catch (ComException e) {
			label.setText("00:00");
		}

		label = new Label(group, SWT.NONE);
		label.setText("Czas rzeczywisty:");
		DateFormat df = new SimpleDateFormat("HH:mm");
		label = new Label(group, SWT.NONE);
		label.setText(df.format(new Date()));

		label = new Label(group, SWT.NONE);
		label.setText("Temperatura:");

		label = new Label(group, SWT.NONE);
		try {
			label.setText(Application.getComApi().readTemp());
		} catch (ComException e) {
			label.setText("0 C");
		}

		parent.redraw();

		// Create the help context id for the viewer's control
		makeActions();
		contributeToActionBars();
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(sychClockAction);
	}

	private void makeActions() {
		sychClockAction = new Action() {
			public void run() {
				DateFormat df = new SimpleDateFormat("HHmm");
				try {
					Application.getComApi().setClock(df.format(new Date()));
				} catch (ComException e) {
					MessageDialog.openError(Display.getDefault()
							.getActiveShell(), "Error during setting hour", e
							.getMessage());
				}
			}
		};
		sychClockAction.setText("Synchronizuj zegar");
		sychClockAction.setToolTipText("Synchronizuj zegar");
		sychClockAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_ELCL_SYNCED));
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {

	}
}